<?php
/**
 * 文章管理
 *
 *
 *
 *
 */
namespace Common\Model;
use Common\Model\BaseModel;

class ArticleModel extends BaseModel{
	/**
	 * 列表
	 *
	 * @param array $condition 检索条件
	 * @param   $page 分页
	 * @return array 数组结构的返回结果
	 */
	public function getArticleList($condition,$page=''){
		
		$condition_str = $this->_condition1($condition);
		 
		$param = array();
		$m=M('article');
 		$param['where'] = $condition_str;
 		$param['order']	= (empty($condition['order'])?'article_sort asc,article_time desc':$condition['order']);
 		 
 		$result =$this->getPage($m,$param['where'],$param['order'],$page);
 		return $result;
	}

 

	 
/**
	 * 构造检索条件
	 *
	 * @param int $id 记录ID
	 * @return string 字符串类型的返回结果
	 */
	private function _condition1($condition){
		$condition_str = '1';

		if ($condition['article_show'] != ''){
			$condition_str .= " and article_show = '". $condition['article_show'] ."'";
		}
		if ($condition['ac_id'] != ''){
			$condition_str .= " and ac_id = '". $condition['ac_id'] ."'";
		}
		if ($condition['ac_ids'] != ''){
			//if(is_array($condition['ac_ids']))$condition['ac_ids']	= implode(',',$condition['ac_ids']);
			$condition_str .= " and ac_id in(". $condition['ac_ids'] .")";
		}
		if ($condition['like_title'] != ''){
			$condition_str .= " and article_title like '%". $condition['like_title'] ."%'";
		}
		if ($condition['strtime'] != '' || $condition['endtime'] != ''){
			$condition_str .= " and article_time between '".strtotime($condition['strtime'])."' and '".strtotime($condition['endtime'])."'";
		}
		
		return $condition_str;
	}
	/**
	 * 取单个内容
	 *
	 * @param int $id ID
	 * @return array 数组类型的返回结果
	 */
	public function getOneArticle($id){
		if (intval($id) > 0){
			$param = array();
			$param['table'] = 'article';
			$param['field'] = 'article_id';
			$param['value'] = intval($id);
			$result = $this->where('article_id='.$param['value'].'')->find();
			//$result = Db::getRow($param);
			return $result;
		}else {
			return false;
		}
	}

	/**
	 * 新增
	 *
	 * @param array $param 参数内容
	 * @return bool 布尔类型的返回结果
	 */
	public function add($param){
		if (empty($param)){
			return false;
		}
		if (is_array($param)){
			$tmp = array();
			foreach ($param as $k => $v){
				$tmp[$k] = $v;
			}
			//$result = Db::insert('article',$tmp);
			$result = M('article')->add($tmp);
			return $result;
		}else {
			return false;
		}
	}

	/**
	 * 更新信息
	 *
	 * @param array $param 更新数据
	 * @return bool 布尔类型的返回结果
	 */
	public function update($param){
		if (empty($param)){
			return false;
		}
		if (is_array($param)){
			$tmp = array();
			foreach ($param as $k => $v){
				$tmp[$k] = $v;
			}
			$where = " article_id = '". $param['article_id'] ."'";
 			$result =$this->where($where)->save($tmp);
			return $result;
		}else {
			return false;
		}
	}

	/**
	 * 删除
	 *
	 * @param int $id 记录ID
	 * @return bool 布尔类型的返回结果
	 */
	public function del($id){
		if (intval($id) > 0){
			$where = " article_id = '". intval($id) ."'";
			//$result = Db::delete('article',$where);
			 $result = $this->where($where)->delete();
			return $result;
		}else {
			return false;
		}
	}
}